<?php
declare (strict_types = 1);

namespace app\admin\controller;


use app\admin\model\Gcategory;
use think\facade\Request;
use think\facade\Db;
use think\facade\View;
//商品列表
class GoodList
{
    public function GoodList()
    {
        //接值

        $goods = Request::get('goods', '', 'trim,htmlspecialchars,strip_tags');
        $if_show = Request::get('if_show/a', [], 'trim,htmlspecialchars,strip_tags');
        $recommended = Request::get('recommended', '2', 'trim,htmlspecialchars,strip_tags');
        $big = Request::get('big', '', 'trim,htmlspecialchars,strip_tags');
        $small = Request::get('small', '', 'trim,htmlspecialchars,strip_tags');
        $cate_id = Request::get('cate_id', '', 'trim,htmlspecialchars,strip_tags');

        $data = [
            'goods' => $goods,
            'if_show' => $if_show,
            'recommended' => $recommended,
            'big' => $big,
            'small' => $small,
            'cate_id' => $cate_id,
        ];

        // 处理排序
        $sort = Request::get('sort', '', 'trim,htmlspecialchars,strip_tags');
        $get = Request::get('', '', 'trim,htmlspecialchars,strip_tags');
        if ('desc' == $sort) {
            View::assign('flag', '↓');
            $get['sort'] = 'asc';
        } elseif ('asc' == $sort) {
            View::assign('flag', '↑');
            unset($get['sort']);
        } else {
            $get['sort'] = 'desc';
        }

        View::assign($data);
        View::assign('get', $get);


        $list = \app\admin\model\Goods::getGoodList($data, input('get.'));
        $empty = "<tr><td colspan='8' align='center'>暂无数据</td></tr>";
        View::assign('empty', $empty);
        View::assign('data', $data);

        $cate_list = Gcategory::getGcategory();
        View::assign('cate_list', $cate_list);
        return View::fetch('', ['list' => $list]);


    }

    public function goodAdd()
    {
        $cates = \app\admin\model\Gcategory::getCateList();  //引入分类
        View::assign('cates', $cates);
        $brand = \app\admin\model\Brand::BrandList();       //引入品牌
        View::assign('brand', $brand);
        if (Request::isPost()) {
            $postParam = Request::post('', '', 'trim,htmlspecialchars,strip_tags');

            $goodsadd = new \app\admin\model\Goods();
            $db = $goodsadd->add($postParam);

            if ($db) {
                echo '成功' . $db;
            } else {
                echo '错误! 原因：' . $goodsadd->getError();
            }
        } else {
            return View::fetch('good_list/good_add');
        }
    }


    public function cate()
    {
        $cate = \app\admin\model\Gcategory::getCateList();
        View::assign('cate', $cate);
        if (Request::isPost()) {
            $post = Request::post('', '', 'trim,htmlspecialchars,strip_tags');

            $cateadd = new \app\admin\model\Gcategory();
            $db = $cateadd->add($post);

            if ($db) {
                echo '成功' . $db;
            } else {
                echo '失败！' . $cateadd->getError();
            }
        }


//        $id = Request::get('id','','trim,htmlspecialchars,strip_tags');
//        $cart = Db::table('ecm_gcategory')
//            ->where('parent_id',$id)
//            ->select()->toArray();
//
//        foreach ($cart as $key=>$value){
//            $cart[$key]['has_next'] = 0;
//
//            $result = Db::table('ecm_gcategory')
//                ->field(true)
//                ->where('parent_id',$value['cate_id'])->find();
//            if($result){
//                $cart[$key]['has_next'] = 1;
//            }
//        }
//
//        $kong = "<tr><td colspan='11'>暂无数据！</td></tr>";
//        View::assign('kong',$kong);
//        View::assign('cart',$cart);
//        View::assign($id);
//        return View::fetch();

        $id = Request::get('id', '0', 'trim,htmlspecialchars,strip_tags');
        $list = \app\admin\model\Gcategory::getcate($id);


        $empty = "<tr><td align='conter' colspan='7'>没有数据</td></rt>";
        View::assign('empty', $empty);
        return View::fetch('good_list/cate', ['list' => $list]);


    }

}
